package labelsystem.Controller;/**
 * @projectName careOlder
 * @Description TODO
 * @author MCG
 * @time 2023/3/30 17:04
 */

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import labelsystem.Pojo.Doctor;
import labelsystem.Pojo.Order;
import labelsystem.Pojo.Text;
import labelsystem.Pojo.Video;
import labelsystem.Service.doctorService;
import labelsystem.utils.returnMSG;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 *description: TODO
 *@author MCG
 *@date 2023/3/30 17:04
 *@Version 1.0
 */
@Api("医生处理模块")
@RestController
@CrossOrigin
public class doctorHandler {
    @Autowired
    doctorService doctorService;
    @Resource
    private RedisTemplate<String,String> redisTemplate;

    @PostMapping("/loginInByPasswordD")
    @ApiOperation("医生登陆:phone,password")
    public returnMSG loginInD(@RequestParam("phone") String phone,@RequestParam("password") String password){
        boolean flag=doctorService.loginInByPassword(phone,password);
        if(flag){
            returnMSG returnMSG= labelsystem.utils.returnMSG.success();
            Doctor doctor=doctorService.queryInfoByTel(Long.parseLong(phone));
            returnMSG.add("doctor",doctor);
            return returnMSG;
        }else{
            returnMSG returnMSG= labelsystem.utils.returnMSG.fail();
            returnMSG.setMsg("密码错误");
            return returnMSG;
        }
    }

    @PostMapping("/resetD")
    @ApiOperation("重置密码")
    public returnMSG loginInByCode(@RequestParam("phone") String phone,@RequestParam("code") String verifyCode,@RequestParam("newpassword") String newpassword,@RequestParam("confirm") String password){
        if(!newpassword.equals(password)){
            returnMSG returnMSG= labelsystem.utils.returnMSG.fail();
            returnMSG.setMsg("请输入一致的密码");
            return returnMSG;
        }
        String code = redisTemplate.opsForValue().get(phone);
        if(doctorService.loginByVerifyCode(code,verifyCode)){
            String reg="^(?=.*\\d)(?=.*[A-z])[\\da-zA-Z]{1,9}$";
            boolean flag=newpassword.matches(reg);
            if(flag){
                Doctor doctor=doctorService.queryInfoByTel(Long.parseLong(phone));
                returnMSG returnMSG= labelsystem.utils.returnMSG.success();
                returnMSG.add("doctor",doctor);
                return returnMSG;
            }else{
                returnMSG returnMSG= labelsystem.utils.returnMSG.fail().fail();
                returnMSG.setMsg("请输入正确的密码格式");
                return returnMSG;
            }

        }else{
            returnMSG returnMSG= labelsystem.utils.returnMSG.fail();
            returnMSG.setMsg("验证码错误");
            return returnMSG;
        }
    }

    @GetMapping("/queryInfoD")
    @ApiOperation("查询个人信息:did")
    public returnMSG queryInfo(@RequestParam("did") String did){
        Doctor doctor=doctorService.queryPersonalInfo(Long.parseLong(did));
        returnMSG returnMSG= labelsystem.utils.returnMSG.success();
        returnMSG.add("doctor",doctor);
        return returnMSG;
    }

    @GetMapping("/queryOrdersD")
    @ApiOperation("查询订单:did")
    public returnMSG queryOrder(@RequestParam("did") String did){
        List<Order> orders=doctorService.selectOrders(Long.parseLong(did));
        returnMSG returnMSG= labelsystem.utils.returnMSG.success();
        returnMSG.add("orders",orders);
        return returnMSG;
    }
    @GetMapping("/queryTextD")
    @ApiOperation("查询发布的文章:did")
    public returnMSG queryText(@RequestParam("did") String did){
        List<Text> texts=doctorService.selectTexts(Long.parseLong(did));
        returnMSG returnMSG= labelsystem.utils.returnMSG.success();
        returnMSG.add("texts",texts);
        return returnMSG;
    }
    @GetMapping("/queryVideoD")
    @ApiOperation("查询发布视频:did")
    public returnMSG queryVideo(@RequestParam("did") String did){
        List<Video> videos=doctorService.selectRadio(Long.parseLong(did));
        returnMSG returnMSG= labelsystem.utils.returnMSG.success();
        returnMSG.add("videos",videos);
        return returnMSG;
    }
    @PostMapping("/registerDoctor")
    @ApiOperation("医生注册功能：name，gender，grade，workplace，specify，phone，password，introduction")
    public returnMSG registerDoctor(@RequestParam("name") String name,@RequestParam("gender") String gender,
                                    @RequestParam("grade") String grade,@RequestParam("workplace") String workplace,
                                    @RequestParam("specify") String specify,@RequestParam("phone") String phone,
                                    @RequestParam("password") String password,@RequestParam("introduction") String introduction){
        String reg="^(?=.*\\d)(?=.*[A-z])[\\da-zA-Z]{1,9}$";
        boolean flag=password.matches(reg);
        if(flag){
            Doctor doctor=doctorService.registerDoctor(name,gender,grade,workplace,specify,Long.parseLong(phone),"",password,Float.parseFloat("60.0"),introduction,"",Float.parseFloat("5.0"));
            returnMSG returnMSG= labelsystem.utils.returnMSG.success();
            returnMSG.add("doctor",doctor);
            return returnMSG;
        }else{
            returnMSG returnMSG= labelsystem.utils.returnMSG.fail();
            returnMSG.setMsg("请输入合法的密码格式");
            return returnMSG;
        }


    }

}
